import mysql from '../../config/mysql'
/**
 * 封装增删改查
 */
class CURD {
    /**
     * 添加
     * @param sql 
     * @param args 
     */
    async add(sql: string, ...args: unknown[]) {
        const res: any = await mysql.execute(sql, args)
        return res[0].affectedRows > 0
    }

    /**
     * 添加
     * @param sql 
     * @param args 
     */
    async addRes(sql: string, ...args: unknown[]) {
        const res: any = await mysql.execute(sql, args)
        return res[0]
    }

    /**
     * 删除
     * @param sql 
     * @param args 
     */
    async delete(sql: string, ...args: unknown[]) {
        const res: any = await mysql.execute(sql, args)
        return res[0].affectedRows > 0
    }

    /**
     * 更新
     * @param sql 
     * @param args 
     */
    async update(sql: string, ...args: unknown[]) {
        const res: any = await mysql.execute(sql, args)
        return res[0].affectedRows > 0;
    }

    /**
     * 查找
     * @param sql 
     * @param args 
     */
    async get(sql: string, ...args: unknown[]) {
        const res: any = await mysql.execute(sql, args)
        return res[0]
    }

    /**
     * 是否存在
     * @param sql 
     * @param args 
     */
    async isExist(sql: string, ...args: unknown[]) {
        return (await this.get(sql, ...args)).length > 0
    }
}

export default new CURD;
